package com.deer.wms.system.manage.web;

import cn.hutool.core.util.ObjectUtil;
import com.deer.wms.intercept.annotation.Authority;
import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import com.deer.wms.project.root.util.StringUtil;
import com.deer.wms.system.manage.model.UserRole;
import com.deer.wms.system.manage.model.UserRoleCriteria;
import com.deer.wms.system.manage.model.permission.PermissionTreeModel;
import com.deer.wms.system.manage.model.role.*;
import com.deer.wms.system.manage.model.user.UserRoleDto;
import com.deer.wms.system.manage.service.PermissionService;
import com.deer.wms.system.manage.service.RoleService;
import com.deer.wms.system.manage.service.UserRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author guo
 * @since 2017/10/07
 */
@Api("角色信息api接口")
@Authority
@RestController
@RequestMapping("/roles")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private UserRoleService userRoleService;

    /**
     * 删除角色信息
     *
     * @param roleId 角色ID
     */
    @GetMapping("/delete")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", required = true)
    })
    public Result delete(Integer roleId) {
        UserRoleCriteria criteria = new UserRoleCriteria();
        criteria.setRoleId(roleId);
        List<UserRoleDto> userRoleDtos = userRoleService.findList(criteria);
        for (UserRole userRole : userRoleDtos) {
            Integer userRoles = userRole.getRoleId();
            if (userRoles.equals(roleId)) {
                return ResultGenerator.genFailResult(CommonCode.ROLE_BOUND, null);
            }
        }
        roleService.removeById(roleId);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 添加角色信息
     *
     * @param create      RoleCreate封装类
     * @param currentUser 当前用户
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", required = true)
    })
    @PostMapping
    public Result save(@RequestBody RoleCreate create, @ApiIgnore @User CurrentUser currentUser) {
        create.setRoleLevel(1);
        Role role = roleService.findBy("role_name", create.getRoleName());
        if (role != null) {
            return ResultGenerator.genFailResult(CommonCode.ROLE_EXISTS, null);
        }
        roleService.addRole(create, currentUser);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 修改角色信息--为角色设置权限
     *
     * @param modify      角色信息
     * @param currentUser 当前操作用户信息
     */
    @ApiOperation(value = "修改角色信息", notes = "修改角色基本信息和权限信息")
    @PutMapping("/{roleId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", required = true)
    })
    public Result modify(@PathVariable Integer roleId, @RequestBody RoleModify modify, @ApiIgnore @User CurrentUser currentUser) {

        Role role = roleService.getById(roleId);
        if (ObjectUtil.isNotNull(role)) {
            // 这里的RoleModify中角色的state就是null的
            // 设置角色状态
            modify.setState(role.getState());
            roleService.modifyRole(modify, currentUser);
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(CommonCode.ROLE_EXISTS, null);
        }
    }

    /**
     * 获取角色详细信息
     *
     * @param roleId 角色ID
     */
    @ApiOperation(value = "获取角色详细信息", notes = "获取角色详细信息，包括角色基本信息和具有的权限信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", required = true)
            , @ApiImplicitParam(name = "roleId", value = "角色信息id", paramType = "path", dataType = "int", defaultValue = "1", required = true)
    })
    @GetMapping("/{roleId}")
    public Result detail(@PathVariable Integer roleId) {

        RoleDetailVO vo = roleService.findRoleByRoleId(roleId);
        return ResultGenerator.genSuccessResult(vo);
    }

    /**
     * 获取指定角色的权限信息
     *
     * @param roleId 角色ID
     */
    @GetMapping("/{roleId}/permission")
    public Result getRolePermission(@PathVariable Integer roleId) {
        List<PermissionTreeModel> treeModels = permissionService.findPermissionRangeTreeFormListByRoleId(roleId);
        return ResultGenerator.genSuccessResult(treeModels);
    }

    /**
     * 获取角色信息
     *
     * @param criteria    角色条件
     * @param currentUser 当前用户
     */
    @ApiOperation(value = "获取角色信息", notes = "获取角色信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", required = true)
    })
    @GetMapping("/list")
    public Result list(RoleCriteria criteria, @ApiIgnore @User CurrentUser currentUser) {

        criteria.setUserId(currentUser.getUserId());

        List<RoleListVO> list = roleService.findRoleFormList(criteria);
        return ResultGenerator.genSuccessResult(list);
    }

    /**
     * 分页获取角色信息
     *
     * @param criteria    查询条件
     * @param currentUser 当前用户
     */
    @ApiOperation(value = "分页获取角色信息", notes = "分页获取角色信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", required = true)
            , @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "path", dataType = "int", defaultValue = "1")
            , @ApiImplicitParam(name = "pageSize", value = "每页显示的条数", paramType = "path", dataType = "int", defaultValue = "20")
    })
    @GetMapping
    public Result pagingList(RoleCriteria criteria, @ApiIgnore @User CurrentUser currentUser) {
        StringUtil.trimObjectStringProperties(criteria);
//        boolean isOperate = currentUser.getCompanyType() == SystemManageConstant.COMPANY_TYPE_OPERATE;
//        criteria.setUserId(isOperate ? null : currentUser.getUserId());
//        criteria.setCompanyId(isOperate ? null : currentUser.getCompanyId());

        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<RoleListVO> list = roleService.findRoleFormList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}
